<template>
    <div style="min-width: 829px">
        <div v-if="$route.path == '/dataReport'">
            <div class="vs-search-box">
                <el-form v-search-box-enter ref="form" :inline="true" :model="paging" label-width="110px">
                    <el-form-item label="年月份：">
                        <el-date-picker v-model="paging.month" type="month" placeholder="选择年月" format="yyyy-MM"
                            value-format="yyyy-MM"> </el-date-picker>
                    </el-form-item>
                    <el-form-item class="vs-right-btns">
                        <el-button @click="reset">重置</el-button>
                        <el-button @click="handleCurrentChange(1)" type="primary">查询</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <div class="vs-table-box">
                <div class="operation-box clear-float">
                    <el-button v-if="utils.getIsShopowner()" style="margin-left: auto" @click="exportFn"
                        icon="el-icon-download" type="primary">导出</el-button>
                </div>
                <el-table v-loading="loading" :data="tableData" header-row-class-name="has-gutter-table"
                    style="width: 100%" border :key="0" show-summary max-height="600" :summary-method="getSummaries">
                    <el-table-column prop="date" label="日期" align="center"></el-table-column>
                    <el-table-column prop="deal" label="成交个数" align="center"></el-table-column>
                    <el-table-column prop="total_money" label="总金额" align="center"></el-table-column>
                    <el-table-column prop="average" label="平均单价" align="center"></el-table-column>
                    <el-table-column prop="repurchase" label="复购个数" align="center"></el-table-column>
                    <el-table-column prop="introduce" label="转介绍" align="center"></el-table-column>
                    <el-table-column prop="enter" label="进店人数" align="center">
                        <template slot-scope="scope">
                            <el-input v-model="scope.row.enter" placeholder="" @blur="blur(scope.row)"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="try_on" label="试戴人数" align="center">
                        <template slot-scope="scope">
                            <el-input v-model="scope.row.try_on" placeholder="" @blur="blur(scope.row)"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="nursing" label="护理人数" align="center"></el-table-column>
                    <el-table-column prop="reserve" label="约修剪人数" align="center"></el-table-column>
                    <el-table-column prop="feedback" label="投诉人数" align="center">
                        <template slot-scope="scope">
                            <el-input v-model="scope.row.feedback" placeholder="" @blur="blur(scope.row)"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="visit" label="回访次数" align="center"></el-table-column>
                    <el-table-column prop="worker" label="上班人数" align="center">
                        <template slot-scope="scope">
                            <el-input v-model="scope.row.worker" placeholder="" @blur="blur(scope.row)"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="repurchase_rate" label="复购率" align="center">
                        <template slot-scope="{ row }"> {{ row.repurchase_rate }}% </template>
                    </el-table-column>
                    <el-table-column prop="try_on_rate" label="试戴率" align="center">
                        <template slot-scope="{ row }"> {{ row.try_on_rate }}% </template>
                    </el-table-column>
                    <el-table-column prop="introduce_rate" label="转介绍率" align="center">
                        <template slot-scope="{ row }"> {{ row.introduce_rate }}% </template>
                    </el-table-column>
                    <el-table-column prop="remark" label="备注" align="center">
                        <template slot-scope="scope">
                            <el-input v-model="scope.row.remark" placeholder="" @blur="blur(scope.row)"></el-input>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
        <router-view></router-view>
    </div>
</template>

<script>
export default {
    data() {
        return {
            shopList: [],
            loading: false,
            paging: {
                page: 1,
                total: 0,
                limit: 10,
                month: '',
            },
            tableData: [],
        };
    },
    created() {
        if (this.$route.path == '/dataReport') {
            this.paging.month = this.$moment(new Date()).format('YYYY-MM');
            this.handleCurrentChange(1);
        }
    },
    methods: {
        getSummaries(param) {
            const { columns, data } = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (index == 0) {
                    sums[index] = '合计';
                    return;
                }
                const values = data.map((item) => Number(item[column.property]));
                if (column.property == 'total_money') {
                    sums[index] = values.reduce((prev, curr) => {
                        const value = Number(curr);
                        if (!isNaN(value)) {
                            return prev + curr;
                        } else {
                            return prev;
                        }
                    }, 0);
                    sums[index] = sums[index].toFixed(2);
                } else if (column.property == 'deal' || column.property == 'repurchase' || column.property == 'introduce' || column.property == 'enter' || column.property == 'try_on' || column.property == 'nursing' || column.property == 'reserve' || column.property == 'feedback' || column.property == 'visit' || column.property == 'worker') {
                    sums[index] = values.reduce((prev, curr) => {
                        const value = Number(curr);
                        if (!isNaN(value)) {
                            return prev + curr;
                        } else {
                            return prev;
                        }
                    }, 0);
                } else {
                    sums[index] = '';
                }
            });
            return sums;
        },
        async blur(row) {
            let { code, data } = await this.$Http.setDataAnalysis({ id: row.id, enter: row.enter, try_on: row.try_on, worker: row.worker, remark: row.remark, feedback: row.feedback });
            if (code) {
                this.$message.success('保存成功!');
                this.handleCurrentChange(1);
            }
        },
        // 导出
        async exportFn() {
            let parms = {
                month: this.paging.month,
            };
            if (!this.paging.month) {
                return this.$message.warning('请选择月份');
            }
            let url = this.utils.data.baseURL + 'store/finance/exportDataAnalysis';
            this.utils.urlToBlob(url, '数据分析报表', parms);
        },
        async repairGetList() {
            let obj = Object.assign({}, this.paging);
            this.loading = true;
            let { code, data } = await this.$Http.getDataAnalysis(obj);
            this.loading = false;
            if (code) {
                this.tableData = data;
                // this.paging.total = data.total;
            }
        },
        reset() {
            //重置
            this.paging = this.$options.data().paging;
            this.handleCurrentChange(1);
        },
        handleSizeChange(val) {
            this.paging.limit = val;
            this.repairGetList();
        },
        handleCurrentChange(val) {
            this.paging.page = val;
            this.repairGetList();
        },
    },
};
</script>

<style lang="scss" scoped>
.image-thumbnail {
    width: 130px;
    height: 130px;
}

.divTable {
    width: 100%;

    div {
        border-bottom: 1px solid #ebeef5;
        border-left: 1px solid #ebeef5;
        border-right: 1px solid #ebeef5;
        height: 50px;
        line-height: 50px;
        padding-left: 30px;
    }
}

table {
    width: 100%;
    border-collapse: collapse;
    border-spacing: 0;
    table-layout: fixed;

    td {
        border: 1px solid #ebeef5;
        text-align: center;
        height: 32px;
        color: #333333;
        padding: 0 5px;
        font-size: 14px;
    }
}
</style>
